﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmBase : Form
    {
        public List<object> Parameter = new List<object>();

        public List<object> Caches = new List<object>();

        public bool IsReturn = false;

        public string PriorForm = "";

        private Timer showTimer;

        private ImageList ilsttitle = new ImageList();
        private Form parent;
        private EventHandler deactiveEvent;

        public FrmBase()
        {
            InitializeComponent();
            int psize = CElectricQuantity.getBatterysize();
            ttlbrhead.Background=ResourceImage.空白标头;
            ibtnstar.UpImage = ResourceImage.抓星较弱;
            ibtnstar.DownImage = ResourceImage.抓星较弱;
            ibtntime.UpImage = ResourceImage.空白tb;
            ibtntime.DownImage = ResourceImage.空白tb;
            pbGPS.Image = ResourceImage.抓星较弱;
            pbSignal.Image = ResourceImage.无信号;
            pbBattery.Image = ResourceImage.电池100;
            ttlbrhead.Refresh();

       
         }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            //设置时间定时
            showTimer = new Timer();
            showTimer.Interval = 30 * 1000;
            showTimer.Tick += new EventHandler(showTimer_Tick);
            showTimer.Enabled = true;

            //设置GPS定位事件
            AppLogic.gpsTraceService.OnDeviceStateChange += gpsTraceService_OnDeviceStateChange;

            AppLogic.netService.OnNetChange += new NetChangeHandler(netService_OnNetChange);

            ShowTitleState();

            showTimer_Tick(null,null);

        }

        void netService_OnNetChange(NetType newNetType)
        {
            ShowSignalState();
        }

        void gpsTraceService_OnDeviceStateChange(string state)
        {
            ShowGPSState(state);
        }

        private void ShowGPSState(string state)
        {
            switch (state)
            {
                case "Position":
                    pbGPS.Image = ResourceImage.抓星较强;
                    break;
                default:
                    pbGPS.Image = ResourceImage.抓星较弱;
                    break;
            }

            pbGPS.Refresh();
        }

        private void ShowTitleState()
        {
            try
            {
                ShowGPSState(AppLogic.gpsTraceService.GPSWorkState);
                ShowSignalState();
                ibtntime.Text = DateTime.Now.ToString("HH:mm");
            }
            catch (Exception)
            {
            }

        }

        private void ShowSignalState()
        {
            if (AppLogic.netService.CurrentNetType == NetType.None)
            {
                pbSignal.Image = ResourceImage.无信号;
            }
            else
            {
                pbSignal.Image = ResourceImage.信号;
            }
        }

        void showTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                //时间
                ibtntime.Text = DateTime.Now.ToString("HH:mm");
                ibtntime.Refresh();

                //电量
                int size = CElectricQuantity.getBatterysize();
                if(size> 90)
                    pbBattery.Image = ResourceImage.电池100;
                else if(size > 80)
                    pbBattery.Image = ResourceImage.电池90;
                else if (size > 70)
                    pbBattery.Image = ResourceImage.电池80;
                else if (size > 60)
                    pbBattery.Image = ResourceImage.电池70;
                else if (size > 50)
                    pbBattery.Image = ResourceImage.电池60;
                else if (size > 40)
                    pbBattery.Image = ResourceImage.电池50;
                else if (size > 30)
                    pbBattery.Image = ResourceImage.电池40;
                else if (size > 20)
                    pbBattery.Image = ResourceImage.电池30;
                else if (size > 10)
                    pbBattery.Image = ResourceImage.电池20;
                else 
                    pbBattery.Image = ResourceImage.电池10;
                pbBattery.Refresh();
            }
            catch (Exception)
            {
            }

        }

        public DialogResult ShowDialog(Form parent, EventHandler deactive)
        {
            deactiveEvent = deactive;
            parent.Deactivate -= deactiveEvent;
            parent.Text = "";
            this.parent = parent;
            this.Deactivate += new EventHandler(BaseFrm_ShowTitleDeactivate);
            this.Activated += new EventHandler(BaseFrm_ShowTitleActivated);
            this.Closing += new CancelEventHandler(BaseFrm_ShowTitleClosing);
            this.Closed += new EventHandler(BaseFrm_ShowTitleClosed);
            return this.ShowDialog();
        }

        void BaseFrm_ShowTitleClosing(object sender, CancelEventArgs e)
        {
            this.Deactivate -= BaseFrm_ShowTitleDeactivate;
        }

        protected void BaseFrm_ShowTitleDeactivate(object sender, EventArgs e)
        {
            MakeFullScreen.toRegularScr(this);
        }

        protected void BaseFrm_ShowTitleActivated(object sender, EventArgs e)
        {
            MakeFullScreen.makeItFullScr(this);
        }


        void BaseFrm_ShowTitleClosed(object sender, EventArgs e)
        {
            if (parent != null)
            {
                parent.Activate();
                Application.DoEvents();
                if (parent != null && deactiveEvent != null)
                {
                    parent.Deactivate += deactiveEvent;
                    parent.Text = FormManage.AppName;
                    parent = null;
                }
            }
        }

        private void FrmBase_Closed(object sender, EventArgs e)
        {
            showTimer.Enabled = false;
            AppLogic.gpsTraceService.OnDeviceStateChange -= gpsTraceService_OnDeviceStateChange;
        }

    }
   }